package com.ftg.product.kucun.controller;

import com.ftg.product.kucun.pojo.Ruku;
import com.ftg.product.kucun.service.RukuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwl
 * Create on 2019/5/7 13:45
 * function
 */
@RestController
@CrossOrigin
@RequestMapping("/ruku")
public class RukuController {
    @Autowired
    RukuService rukuservice;

    @PostMapping("/show")
    public Map<String, Object> showall(@RequestBody Ruku pageH) {
        PageHelper.startPage(pageH.getPage(),pageH.getLimit());
        List<Ruku> list = rukuservice.showall(pageH);
        Map<String, Object> map = new HashMap<>();
        PageInfo<Ruku> page=new PageInfo<>(list);
        map.put("msg","success");
        map.put("code", "0");
        map.put("data", list);
        map.put("count",page.getTotal());
        return map;
    }
    @PostMapping("/insert")
    public int insert(@RequestBody Ruku ruku){
        return rukuservice.insert(ruku);
    }
    @PostMapping("/update")
    public int update(@RequestBody Ruku ruku){
        return rukuservice.update(ruku);

    }
    @DeleteMapping("/delete/{rid}")
    public int delete(@PathVariable("rid") String rid){
        return rukuservice.delete(rid);

    }
    /**
     * @RequestBody Admin pageH
     * 分页模糊查询
     *
     * */
    @PostMapping("/showone")
    public Map<String,Object> showAll(@RequestBody Ruku pageH){
        //判断传入的数据是否为NULL，如果为NULL，则把他的值改为""
        if(pageH.getWrite() == null){
            pageH.setWrite("");
        }
        //开始分页Admin>集合
        PageHelper.startPage(pageH.getPage(),pageH.getLimit());
        //用List< 去存service层调用的方法
        List<Ruku> list = rukuservice.show(pageH);
        //包装查询后的结果
        PageInfo<Ruku> page = new PageInfo<>(list);
        //实例化map结果集
        Map<String,Object> map = new HashMap<>();
        //将list中的数据放入map中
        map.put("data",list);
        //code码 为0
        map.put("code","0");
        //成功信息为success
        map.put("msg","success");
        //将总数据条数放入map中
        map.put("count",page.getTotal());
        //返回map
        return map;
    }
}
